package com.xuecheng.system.controller;

import com.xuecheng.commons.model.dto.CompanyApproveDto;
import com.xuecheng.commons.model.dto.CompanyDto;
import com.xuecheng.commons.model.dto.CompanyListDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.AuthInfo;
import com.xuecheng.commons.model.vo.CompanyVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.system.domain.Company;
import com.xuecheng.system.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 教育机构前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CompanyController {

    @Autowired
    private CompanyService  companyService;

    // 机构查询
    @GetMapping("/company/mine")
    public ResponseResult mine(){
        // 1.从线程内获取 authInfo
        AuthInfo authInfo = AuthInfoHolder.getAuthInfo();
        // 2.根据企业id查询数据库
        Company company = companyService.getById(authInfo.getCompanyId());
        // 3.封装并返回vo （复制工具类）
        CompanyVo vo = BeanHelper.copyProperties(company, CompanyVo.class);
        return ResponseResult.okResult(vo);
    }

    //机构资料提交
    @PostMapping("/company")
    public ResponseResult saveCompany(@RequestBody CompanyDto dto){
        //调用service
        return companyService.saveCompany(dto);
    }

    //机构/个人组织列表
    @PostMapping("/company/list")
    public ResponseResult findCompanyPage(@RequestBody CompanyListDto dto, PageRequest params){
        //调用service
        return companyService.findCompanyPage(dto,params);
    }

    //机构审核
    @PostMapping("/company/approve")
    public ResponseResult approve(@RequestBody CompanyApproveDto dto){
        //调用service
        return companyService.approve(dto);
    }

    //根据id查询企业
    @GetMapping("/company/{id}")
    public ResponseResult findCompanyById(@PathVariable("id") String id){
        //1.调用service查询
        Company company = companyService.getById(id);
        //2.转vo
        CompanyVo companyVo = BeanHelper.copyProperties(company, CompanyVo.class);
        //3.返回结果
        return ResponseResult.okResult(companyVo);
    }
}
